<template>
  <div class="app-container">
    <el-container>
      <el-main>
    <div style="margin-left: 35%;margin-right: 30%">
    <el-form :model="queryParams" ref="queryForm" size="small" v-show="showSearch" label-width="100px">


      <el-form-item label="仓库" prop="storeId">
        <el-select v-model="queryParams.storeId" clearable placeholder="请选择">
          <el-option
            v-for="item in stores"
            :key="item.storeName"
            :label="item.storeName"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>


      <el-form-item label="出入库时间" prop="busiDate">
        <el-date-picker clearable
                        v-model="queryParams.busiDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择出入库时间">
        </el-date-picker>
      </el-form-item>


      <el-form-item label="备注" prop="operatorId">

        <el-input
          v-model="queryParams.operatorId"
          type="textarea"
          placeholder="请输入备注"
          clearable
          @keyup.enter.native="handleQuery"
        />

      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['goods:bill:add']"
        >查看明细</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table :data="billList" style="width: 100%" border>
      <el-table-column prop="storeId" label="物品">
      </el-table-column>
      <el-table-column prop="price" label="价格">
        <template slot-scope="scope">
        <el-input
          size="mini"
          v-model="scope.row.price"
        >
        </el-input>
        </template>
      </el-table-column>
      <el-table-column prop="amount" label="数量">
        <template slot-scope="scope">
        <el-input
          size="mini"
          v-model="scope.row.amount"
        >
        </el-input>
        </template>
      </el-table-column>

      <el-table-column prop="totalSum" label="小计" >
        <template slot-scope="scope">
        {{ scope.row.amount * scope.row.price }}
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            icon="el-icon-minus"
            type="danger"
            @click="minusHandle(scope.$index)"
          >
          </el-button>
        </template>
      </el-table-column>


      <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
        <el-row>
          <el-table v-loading="loading" :data="goodsList" @selection-change="handleSelectionChange">
            <el-table-column label="物品名称" align="center" prop="goodsName" />
            <el-table-column label="封面" align="center" prop="goodsCover" />
            <el-table-column label="分类" align="center" prop="categoryId" />
            <el-table-column label="品牌" align="center" prop="brand" />
            <el-table-column label="规格" align="center" prop="spec" />
            <el-table-column label="描述" align="center" prop="goodsDesc" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                  v-hasPermi="['goods:goods:edit']"
                >修改</el-button>
              </template>
            </el-table-column>
          </el-table>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
        </el-row>
      </el-dialog>

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    </el-form>
    </div>
      </el-main>
    </el-container>
  </div>
</template>

<script>
import { listBill, getBill, delBill, addBill, updateBill ,getStore} from "@/api/business/stockbill";
import { listStore } from '@/api/business/store'
export default {
  name: "Bill",
  data() {
    return {
      amount:0,
      goodList:[],
      stores:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 出入库单据表格数据
      billList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      storeParams:{
        pageNum: 1,
        pageSize: 10,
        storeName: null,
        storeAddress: null,
        state: null,
        keywords:null
      },
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: null,
        storeId: null,
        busiDate: null,
        status: null,
        operateDate: null,
        operatorId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getStores();
  },
  methods: {
    /** 查询出入库单据列表 */
    getList() {
      this.loading = true;
      listBill(this.queryParams).then(response => {
        this.billList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getStores() {
      listStore(this.storeParams).then(response => {
        console.log(response)
        this.stores = response.rows;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        type: null,
        storeId: null,
        busiDate: null,
        status: 0,
        operateDate: null,
        operatorId: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加出入库单据";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBill(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改出入库单据";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBill(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBill(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除出入库单据编号为"' + ids + '"的数据项？').then(function() {
        return delBill(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('goods/bill/export', {
        ...this.queryParams
      }, `bill_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
